<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>含标识API前端</title>
    <style>
        :root {
            --primary-color: #4CAF50;
            --primary-hover: #45a049;
            --token-color: #2196F3;
            --token-hover: #0b7dda;
            --border-color: #ddd;
            --section-bg: #f9f9f9;
        }

        body {
            font-family: 'Segoe UI', Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
            color: #333;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 1px solid var(--border-color);
        }

        .main-container {
            display: flex;
            gap: 30px;
        }

        .left-panel, .right-panel {
            flex: 1;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 20px;
        }

        .section {
            margin-bottom: 25px;
            padding: 15px;
            border-radius: 6px;
            background-color: var(--section-bg);
        }

        .section-title {
            margin-top: 0;
            color: #2c3e50;
            border-bottom: 1px solid var(--border-color);
            padding-bottom: 10px;
        }

        .input-group {
            display: flex;
            align-items: center;
            margin-bottom: 12px;
        }

        label {
            display: inline-block;
            width: 100px;
            font-weight: 500;
        }

        input {
            padding: 8px 12px;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            width: 80px;
            font-size: 14px;
        }

        .button-group {
            display: flex;
            flex-direction: column;
            gap: 10px;
            margin-top: 15px;
        }

        button {
            padding: 10px 15px;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.3s;
            width: 100%;
        }

        .primary-btn {
            background-color: var(--primary-color);
        }

        .primary-btn:hover {
            background-color: var(--primary-hover);
        }

        .token-btn {
            background-color: var(--token-color);
        }

        .token-btn:hover {
            background-color: var(--token-hover);
        }

        #result {
            border: 1px solid var(--border-color);
            padding: 20px;
            border-radius: 6px;
            background-color: white;
            min-height: 300px;
            white-space: pre-wrap;
            font-family: 'Courier New', monospace;
            font-size: 14px;
            overflow-y: auto;
            max-height: 400px;
        }

        .result-title {
            margin-top: 30px;
            color: #2c3e50;
        }

        @media (max-width: 768px) {
            .main-container {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>含标识API前端</h1>
    </div>

    <div class="main-container">
        <!-- 左侧面板 - 普通请求 -->
        <div class="left-panel">
            <div class="section">
                <h2 class="section-title">普通请求</h2>

                <div class="input-group">
                    <label for="messagePage">页码:</label>
                    <input type="number" id="messagePage" value="1" min="1">
                </div>
                <div class="input-group">
                    <label for="messagePerPage">每页数量:</label>
                    <input type="number" id="messagePerPage" value="10" min="1">
                </div>
                <div class="button-group">
                    <button class="primary-btn" onclick="getMessages()">获取消息列表</button>
                </div>

                <div class="input-group">
                    <label for="messageId">消息ID:</label>
                    <input type="number" id="messageId" value="1" min="1">
                </div>
                <div class="button-group">
                    <button class="primary-btn" onclick="getMessageById()">获取单条消息</button>
                </div>
            </div>
        </div>

        <!-- 右侧面板 - 带标识请求 -->
        <div class="right-panel">
            <div class="section">
                <h2 class="section-title">带标识的请求</h2>

                <div class="input-group">
                    <label for="token">Token:</label>
                    <input type="text" id="token" value="test123" placeholder="输入token">
                </div>

                <div class="input-group">
                    <label for="messagePageToken">页码:</label>
                    <input type="number" id="messagePageToken" value="1" min="1">
                </div>
                <div class="input-group">
                    <label for="messagePerPageToken">每页数量:</label>
                    <input type="number" id="messagePerPageToken" value="10" min="1">
                </div>
                <div class="button-group">
                    <button class="token-btn" onclick="getMessagesWithToken()">获取带标识的消息列表</button>
                </div>

                <div class="input-group">
                    <label for="messageIdToken">消息ID:</label>
                    <input type="number" id="messageIdToken" value="1" min="1">
                </div>
                <div class="button-group">
                    <button class="token-btn" onclick="getMessageByIdWithToken()">获取带标识的单条消息</button>
                </div>
            </div>
        </div>
    </div>

    <h2 class="result-title">API返回结果:</h2>
    <div id="result">点击上面的按钮测试API，结果将显示在这里...</div>

    <script>
        const apiBaseUrl = 'http://127.0.0.1:9999/api';

        function displayResult(data) {
            document.getElementById('result').textContent = JSON.stringify(data, null, 2);
        }

        function handleError(error) {
            console.error('Error:', error);
            document.getElementById('result').textContent = '请求出错: ' + error.message;
        }

        // 获取消息列表
        function getMessages() {
            const page = document.getElementById('messagePage').value;
            const perPage = document.getElementById('messagePerPage').value;
            const url = `${apiBaseUrl}/messages?page=${page}&per_page=${perPage}`;

            fetch(url)
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    return response.json();
                })
                .then(data => displayResult(data))
                .catch(error => handleError(error));
        }

        // 获取单条消息
        function getMessageById() {
            const messageId = document.getElementById('messageId').value;
            const url = `${apiBaseUrl}/messages/${messageId}`;

            fetch(url)
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    return response.json();
                })
                .then(data => displayResult(data))
                .catch(error => handleError(error));
        }

        // 获取带标识的消息列表
        function getMessagesWithToken() {
            const page = document.getElementById('messagePageToken').value;
            const perPage = document.getElementById('messagePerPageToken').value;
            const token = document.getElementById('token').value;
            const url = `${apiBaseUrl}/messages?page=${page}&per_page=${perPage}&token=${encodeURIComponent(token)}`;

            fetch(url)
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    return response.json();
                })
                .then(data => displayResult(data))
                .catch(error => handleError(error));
        }

        // 获取带标识的单条消息
        function getMessageByIdWithToken() {
            const messageId = document.getElementById('messageIdToken').value;
            const token = document.getElementById('token').value;
            const url = `${apiBaseUrl}/messages/${messageId}?token=${encodeURIComponent(token)}`;

            fetch(url)
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    return response.json();
                })
                .then(data => displayResult(data))
                .catch(error => handleError(error));
        }
    </script>
</body>
</html>